package com.neutech.maplestory.client;

import com.neutech.maplestory.entity.*;
import com.neutech.maplestory.util.MapleStoryFrame;
import com.neutech.maplestory.util.MusicThread;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/11/27 9:33
 */
public class MapleStoryClient extends MapleStoryFrame {

    public Background background = new Background(this,0,-580);
    /**
     * 设计模式：调停者（中介者）设计模式
     */
    public Hero hero = new Hero(this,800,500);

    public ItemPackage itemPackage = new ItemPackage(this,1400,450);


    /**
     * 存放弓箭的容器
     */
    public List<Arrow> arrows = new CopyOnWriteArrayList<>();

    public List<Mob> mobs = new CopyOnWriteArrayList<>();

    public List<Ground> grounds = new ArrayList<>();

    /*public Item item = new Item(this,400,400,6);*/

    public List<Item> items = new CopyOnWriteArrayList<>();

    public List<Power> powers = new CopyOnWriteArrayList<>();

    {
        for (int i = 0; i < 10; i++) {
            Mob mob = new Mob(this,700 + (i * 80),500,0);
            mobs.add(mob);
        }
    }

    {
        // 左上台
        grounds.add(new Ground(this,0,295,1128,40));
        grounds.add(new Ground(this,0,355,340,50));
        grounds.add(new Ground(this,200,480,500,50));
        // 斜坡功能
        for (int i = 0; i < 75; i++) {
            // 图片宽高比为 450 320  简化比为 3 : 2
            grounds.add(new Ground(this,1128 + (i * 6),300 + (i * 4),6,4));
        }
        // 地面
        grounds.add(new Ground(this,0,780,1547,240));
        grounds.add(new Ground(this,1547,720,90,280));
        grounds.add(new Ground(this,1637,660,413,350));
        grounds.add(new Ground(this,2050,720,413,300));
        // 中间台
        grounds.add(new Ground(this,1190,720,90,280));
        grounds.add(new Ground(this,1280,660,90,350));
        grounds.add(new Ground(this,1370,600,1050,400));
    }

    @Override
    public void init(String title) {
        super.init(title);
        // 添加键盘监听器
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
            }

            @Override
            public void keyReleased(KeyEvent e) {
                hero.keyReleased(e);
            }
        });
//        new MusicThread("bgm.mp3",true).start();
    }

    @Override
    public void paint(Graphics g) {
        background.draw(g);
        for (int i = 0; i < grounds.size(); i++) {
            Ground ground = grounds.get(i);
            ground.draw(g);
        }
        for (int i = 0; i < arrows.size(); i++) {
            Arrow arrow = arrows.get(i);
            arrow.draw(g);
            arrow.hit(mobs);
        }
        mobs.forEach(mob -> mob.draw(g));
        items.forEach(item ->item.draw(g));
        hero.draw(g);
        hero.pickup(items);

        powers.forEach(power -> power.draw(g));

        itemPackage.draw(g);

        Font f = g.getFont();
        g.setFont(new Font("微软雅黑",Font.BOLD,35));
        g.drawString("x:" + hero.x,100,200);
        g.drawString("y:" + hero.y,100,250);
        g.drawString("mobs.size():" + mobs.size(),100,300);
        g.drawString("hero.vt:" + hero.vt,100,350);
        g.drawString("grounds.get(74).x:" + grounds.get(74).x,100,400);
        g.drawString("grounds.get(74).y:" + grounds.get(74).y,100,400);
        g.setFont(f);
    }
    public static void main(String[] args) {
        new MapleStoryClient().init("MapleStory_v1.0");
    }
}
